package com.aqielife.common.objectpool;

import cn.hutool.core.math.Money;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import java.math.BigDecimal;

/**
 * ObjectPool   : 对象池 GenericObjectPool GenericKeyedObjectPool
 * Factory      :  创建 管理PooledObject
 * PooledObject : 一般用DefaultPooledObject
 */
@Slf4j
public class MoneyPooledObjectFactory
        implements PooledObjectFactory<Money> {


    @Override
    public PooledObject<Money> makeObject() throws Exception {
        DefaultPooledObject<Money> object = new DefaultPooledObject<>(
                new Money( new BigDecimal("1"))
        );
        log.info("makeObject..state = {}", object.getState());
        return object;
    }

    @Override
    public void destroyObject(PooledObject<Money> p) throws Exception {
        log.info("destroyObject..state = {}", p.getState());
    }

    @Override
    public boolean validateObject(PooledObject<Money> p) {
        log.info("validateObject..state = {}", p.getState());
        return true;
    }

    @Override
    public void activateObject(PooledObject<Money> p) throws Exception {
        log.info("activateObject..state = {}", p.getState());
    }

    @Override
    public void passivateObject(PooledObject<Money> p) throws Exception {
        log.info("passivateObject..state = {}", p.getState());
    }
}
